package com.cg.controller;

import com.cg.common.CategoryAndPage;
import com.cg.common.JWTUtils;
import com.cg.common.Result;
import com.cg.entity.Category;
import com.cg.entity.Event;
import com.cg.entity.EventCategoryName;
import com.cg.entity.EventLike;
import com.cg.exception.BusinessException;
import com.cg.service.EventService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/qualityManager/event")
@RestController
public class EventController {
    @Autowired
    private EventService eventService;

    //查询所有
    @RequestMapping("/selectList")
    public Result<List<EventCategoryName>> selectList(@RequestHeader("Authorization") String token){
        List<EventCategoryName> list=eventService.selectList(JWTUtils.getAccount(token));
        return new Result<>(200,"查询成功",list);
    }

    //新增任务
    @RequestMapping("/insert")
    public Result<String> insert(@RequestBody Event event,@RequestHeader("Authorization") String token){
        event.setCreatedBy(JWTUtils.getAccount(token));
        int a=eventService.insert(event);
        return new Result<>(1,"新增成功","");
    }
//更新任务
    @RequestMapping("/update")
    public Result<String>update(@RequestBody Event event,@RequestHeader("Authorization") String token){
        event.setCreatedBy(JWTUtils.getAccount(token));
        int a=eventService.update(event);
        if (a==0){
            return new Result<String>(0,"修改失败","修改失败");
        }
        return new Result<>(200,"修改成功","修改成功");
    }


    //删除事件
    @RequestMapping("/deleteOne")
    public Result<String>delete(@RequestBody Event event){
        int a=eventService.delete(event.getId());
        if (a==0) {
            return new Result<String>(0,"删除失败","删除失败");
        }
        return new Result<>(1,"删除成功","删除成功");
    }
    @RequestMapping("/selectListByDateDesc")
    public Result<List<Event>>selectListByDateDesc(@RequestBody CategoryAndPage categoryAndPage) {
        List<Event> list = eventService.selectListByDateDesc(categoryAndPage.getCategory().getCategoryName());
        return new Result<>(1, "", list);
    }

    @RequestMapping("/selectone")
    public Result<Event>selectone(@RequestBody Event event){
        Event event1=new Event();
        event1=eventService.selectone(event.getId());
        return new Result<Event>(1,"",event1);
    }
//模糊查询
    @RequestMapping("/selectLike")
    public Result<List<EventCategoryName>>selectLike(@RequestBody EventLike eventLike,@RequestHeader("Authorization") String token) {
            eventLike.setCreatedBy(JWTUtils.getAccount(token));
            List<EventCategoryName> list = eventService.selectLike(eventLike.getCreatedBy(),eventLike.getCategoryId(), eventLike.getTitle(), eventLike.getLevel(), eventLike.getStatus());
            return new Result<>(200, "", list);
    }
}
